//
//  AllStoryViewController.m
//  EDPS
//
//  Created by 张维扬 on 15/11/26.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "AllStoryViewController.h"
#import "ExploreCollectionViewCell.h"
#import "ZWY_NetHandle.h"
#import "ExploreModel.h"
#import "imgNameModel.h"
@interface AllStoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableArray *userArr;
@end

@implementation AllStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(253, 244, 233, 1);
    self.navigationController.navigationBarHidden = NO;
    // 获取数据
    [self getData];
    // 创建collectionView
    [self createCollectionView];
    
}
#pragma mark - 请求数据
- (void)getData
{
    NSString *url = @"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0";
    [ZWY_NetHandle getURL:url Cookie:nil parameter:nil result:^(id result) {
        self.dataArr = [ExploreModel transformWithArray:result[@"data"][@"hot_spot_list"]];
        // 用户数据
        self.userArr = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"][@"hot_spot_list"]) {
            [self.userArr addObject:dic[@"user"]];
        }
        self.userArr = [imgNameModel transformWithArray:self.userArr];
        [self.collectionView reloadData];
    }];
    
}
#pragma mark - 创建CollectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout * Layout = [[UICollectionViewFlowLayout alloc] init];
    Layout.itemSize = CGSizeMake((ScreenWidth - 30) / 2, (ScreenWidth - 30) / 2);
    Layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    Layout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 50) collectionViewLayout:Layout];
    self.collectionView.backgroundColor = RGBA(253, 244, 233, 1);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ExploreCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    [self.view addSubview:self.collectionView];
}
#pragma mark - collectionView协议方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ExploreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.row];
    cell.imgName_Model = self.userArr[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
